clear all
set more off
set maxvar 20000

*Define directory with input data
cd "Q:"

*Load RAND longitudinal data
Use hrsrandlongdata_1994_2018

*Merge self-employment classification
merge 1:1 hhidpn waveyr using rand_94_18_with_coded_narrs
drop _merge

*Merge RAND imputation file
merge 1:1 hhid pn waveyr using hrsrandimpdata_1994_2020
keep if _merge==3
drop _merge

*Merge CPI-W 
gen year_m1=year-1
merge m:1 year_m1 using cpi_w
drop if _merge==2
drop _merge

*Merge in 2020 early Tracker file
merge 1:1 hhid pn waveyr using tracker_2020_early 
keep if _merge==3
drop _merge

*Merge RAND FAT data
merge 1:1 hhid pn waveyr using hrsrandfatdata1996_2018
drop if _merge==2
drop _merge

*Merge in 2018 SS weigts
merge 1:1 hhid pn waveyr using hrssswtsdata_2018
drop if _merge==2
drop _merge

*Define last calendar year variable
gen lcy=iwyear-1

*Merge 2016 SER and 2016 DER to RAND file in current year (CY) and last calendar year (LCY)
merge m:1 hhid pn iwyear using hrsderdata_2016, keepusing(hhid pn iwyear tot_se_earn non_se_earn se_topcoded non_se_topcoded)
drop if _merge==2
rename _merge merge_der_cy

merge m:1 hhid pn iwyear using hrsserdata_2016, keepusing(hhid pn iwyear SE EARN)
drop if _merge==2
rename _merge merge_ser_cy

merge m:1 hhid pn lcy using hrsderdata_2016, keepusing(hhid pn lcy tot_se_earn_lcy non_se_earn_lcy se_topcoded_lcy non_se_topcoded_lcy)
drop if _merge==2
rename _merge merge_der_lcy

merge m:1 hhid pn lcy using hrsserdata_2016, keepusing(hhid pn lcy SE_lcy EARN_lcy)
drop if _merge==2
rename _merge merge_ser_lcy

rename (tot_se_earn non_se_earn SE EARN) =_cy

*Save
save se_analysis_data_1996_2018, replace

